<?php
class ControllerLocalisationTaxRate extends AdminController {

	public function index() {
		M('localisation/tax_rate');
		$this->getList();
	}

	public function insert() {
		M('localisation/tax_rate');

		if ($this->request->isPost() && $this->validateForm()) {
			$this->model_localisation_tax_rate->addTaxRate($this->request->post);

			$this->session->set_flashdata('success', $this->_['text_success']);
			$this->redirect(UA('localisation/tax_rate'));
		}

		$this->getForm();
	}

	public function update() {
		M('localisation/tax_rate');

		if ($this->request->isPost() && $this->validateForm()) {
			$this->model_localisation_tax_rate->editTaxRate($this->request->get['tax_rate_id'], $this->request->post);

			$this->session->set_flashdata('success', $this->_['text_success']);
			$this->redirect(UA('localisation/tax_rate'));
		}

		$this->getForm();
	}

	public function delete() {
		M('localisation/tax_rate');

		if (isset($this->request->post['selected']) && $this->validateDelete()) {
			foreach ($this->request->post['selected'] as $tax_rate_id) {
				$this->model_localisation_tax_rate->deleteTaxRate($tax_rate_id);
			}

			$this->session->set_flashdata('success', $this->_['text_success']);
			$this->redirect(UA('localisation/tax_rate'));
		}

		$this->getList();
	}

	protected function getList() {
		$this->document->setTitle($this->_['heading_title']);

		$qf = new Query_filter();
		$tax_rate_total = $this->model_localisation_tax_rate->getTotalTaxRates();
		$this->data['tax_rates'] = $this->model_localisation_tax_rate->getTaxRates($qf);

		$this->data['pagination'] = $this->pagination($tax_rate_total)->render();

		$this->display('localisation/tax_rate_list.tpl');
	}

	protected function getForm() {
		$this->document->setTitle($this->_['heading_title']);

		if (isset($this->request->get['tax_rate_id'])) {
			$this->data['action'] = UA('localisation/tax_rate/update', 'tax_rate_id=' . $this->request->get['tax_rate_id']);
			if (!$this->request->isPost()) {
				$this->vo->data = $this->model_localisation_tax_rate->getTaxRate($this->request->get['tax_rate_id']);
			}	
		} else {
			$this->data['action'] = UA('localisation/tax_rate/insert');
		}

		if (isset($this->request->post['tax_rate_customer_group'])) {
			$this->data['tax_rate_customer_group'] = $this->request->post['tax_rate_customer_group'];
		} elseif (isset($this->request->get['tax_rate_id'])) {
			$this->data['tax_rate_customer_group'] = $this->model_localisation_tax_rate->getTaxRateCustomerGroups($this->request->get['tax_rate_id']);
		} else {
			$this->data['tax_rate_customer_group'] = array();
		}

		M('sale/customer_group');
		$this->data['customer_groups'] = $this->model_sale_customer_group->getCustomerGroups();

		M('localisation/geo_zone');
		$this->data['geo_zones'] = $this->model_localisation_geo_zone->getGeoZones();

		$this->display('localisation/tax_rate_form.tpl');
	}

	protected function validateForm() {

		$pass = true;
		if (!range_length($this->request->post['name'], 3, 32)) {
			$this->setMessage('error_name', $this->_['error_name']);
			$pass = false;
		}

		if (!$this->request->post['rate']) {
			$this->setMessage('error_rate', $this->_['error_rate']);
			$pass = false;
		}

		return $pass;
	}

	protected function validateDelete() {

		M('localisation/tax_class');

		foreach ($this->request->post['selected'] as $tax_rate_id) {
			$tax_rule_total = $this->model_localisation_tax_class->getTotalTaxRulesByTaxRateId($tax_rate_id);

			if ($tax_rule_total) {
				$this->setMessage('error_warning', sprintf($this->_['error_tax_rule'], $tax_rule_total));
				return false;
			}
		}
		return true;
	}
}
?>